package cn.tedu.admin.account.controller;

import cn.tedu.admin.account.pojo.param.UserAddNewParam;
import cn.tedu.admin.account.pojo.param.UserUpdateInfoParam;
import cn.tedu.admin.account.pojo.vo.UserListItemVO;
import cn.tedu.admin.account.pojo.vo.UserStandardVO;
import cn.tedu.admin.account.service.IUserService;
import cn.tedu.common.pojo.vo.PageData;
import cn.tedu.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    IUserService iUserService;

    @PostMapping("add-new")
    @PreAuthorize("hasAuthority('/account/user/add-new')")
    @ApiOperation("添加用户功能")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(UserAddNewParam userAddNewParam){
        iUserService.addNew(userAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/password/update")
    @PreAuthorize("hasAuthority('/account/user/edit')")
    @ApiOperation("修改用户密码")
    @ApiOperationSupport(order = 200)
    public JsonResult updatePassword(Long id, String newPassword){
        iUserService.updatePassword(id,newPassword);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/avatar/update")
    @PreAuthorize("hasAuthority('/account/user/edit')")
    @ApiOperation("修改用户头像")
    @ApiOperationSupport(order = 200)
    public JsonResult updateAvatar(Long id,String userUrl){
        iUserService.updateUserurl(id,userUrl);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/Info/update")
    @PreAuthorize("hasAuthority('/account/user/edit')")
    @ApiOperation("修改用户基本信息")
    @ApiOperationSupport(order = 210)
    public JsonResult updateInfo(Long id, UserUpdateInfoParam userUpdateInfoParam){
        iUserService.updateInfo(id, userUpdateInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/setEnable/update")
    @PreAuthorize("hasAuthority('/account/user/edit')")
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 220)
    public JsonResult setEnable(Long id){
        iUserService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/setDisable/update")
    @PreAuthorize("hasAuthority('/account/user/edit')")
    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 230)
    public JsonResult setDisable(Long id){
        iUserService.setDisable(id);
        return JsonResult.ok();
    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('/account/user/query')")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 330)
    public JsonResult list(Integer pageNum){
        PageData<UserListItemVO> list = iUserService.list(pageNum);
        return JsonResult.ok(list);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('/account/user/query')")
    @ApiOperation("根据ID查询用户")
    @ApiOperationSupport(order = 320)
    public JsonResult list(Long id){
        UserStandardVO standardById = iUserService.getStandardById(id);
        return JsonResult.ok(standardById);
    }



}


